/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.theydream.qeechain.main;

import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.theydream.qeechain.R;
import com.theydream.qeechain.globaldata.GlobalData;
import com.theydream.qeechain.globaldata.definition.Unlocker;


/**
 * author Sum
 * Caught Pets are shown in a list view
 */
public class ItemCollected extends ListActivity {
	static Context mContext;
	private static List<Unlocker> unlocker = null;
	

    private static class EfficientAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        public EfficientAdapter(Context context) {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            mContext=context;
        	mInflater = LayoutInflater.from(context);
        	
        }

        public int getCount() {
            return unlocker.size();
        }

        /**
         * Since the data comes from an array, just returning the index is
         * sufficent to get at the data. If we were using a more complex data
         * structure, we would return whatever object represents one row in the
         * list.
         *
         * @see android.widget.ListAdapter#getItem(int)
         */
        public Object getItem(int position) {
            return position;
        }

        /**
         * Use the array index as a unique id.
         *
         * @see android.widget.ListAdapter#getItemId(int)
         */
        public long getItemId(int position) {
            return position;
        }

        /**
         * Make a view to hold each row.
         *
         * @see android.widget.ListAdapter#getView(int, android.view.View,
         *      android.view.ViewGroup)
         */
        public View getView(int position, View convertView, ViewGroup parent) {
            // A ViewHolder keeps references to children views to avoid unneccessary calls
            // to findViewById() on each row.
            ViewHolder holder;
            String[] DATA=new String[unlocker.size()];

            // When convertView is not null, we can reuse it directly, there is no need
            // to reinflate it. We only inflate a new View when the convertView supplied
            // by ListView is null.
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list_item_icon_text, null);

                // Creates a ViewHolder and store references to the two children views
                // we want to bind data to.
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.text);
                holder.icon = (ImageView) convertView.findViewById(R.id.icon);

                convertView.setTag(holder);
            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }          
         
            for (int i=0; i<DATA.length;i++)
            {
            	DATA[i]=unlocker.get(i).getName();
//            	caughtItemListClass.getItemList().get(i).getPet().getName();
            	
            }
            // Bind the data efficiently with the holder.
            holder.text.setText(DATA[position]);
            //Drawable drawable=mContext.getResources().getDrawable(mContext.getResources().getIdentifier(caughtItemListClass.getItemList().get(position).getPet().getPic(),"drawable",this.getClass().getPackage().toString()));
            Drawable drawable=mContext.getResources().getDrawable(mContext.getResources().getIdentifier(unlocker.get(position).getPic(), "drawable", "com.theydream.qeechain"));

            if (null!=drawable)
            {
            	holder.icon.setImageDrawable(drawable);
            }
            else
            {
            	holder.icon.setImageResource(R.drawable.no_image);
            }
            return convertView;
        }

        static class ViewHolder {
            TextView text;
            ImageView icon;
            
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        unlocker = GlobalData.getInstance().getDbhelper().getUnlocker();
        	
//        	Dialog dialog=new Dialog(this);
//        	dialog.setTitle(title)
//        	Toast.makeText(getApplicationContext(), "You have not found any elements. Find them through battles!", Toast.LENGTH_SHORT).show();
        
        
        EfficientAdapter mAdapter=new EfficientAdapter(this);
        setListAdapter(mAdapter);
    }
    
    @Override
    public void onResume()	{
    	super.onResume();
    	unlocker = GlobalData.getInstance().getDbhelper().getUnlocker();
        if (0==unlocker.size())	{
        	AlertDialog.Builder builder = new AlertDialog.Builder(this);
        	builder.setMessage("No elements Found. Find them through battles!")
        	       .setCancelable(false)
        	       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        	           public void onClick(DialogInterface dialog, int id) {
        	                ItemCollected.this.finish();
        	           }
        	       })
        	       .setNegativeButton("No", new DialogInterface.OnClickListener() {
        	           public void onClick(DialogInterface dialog, int id) {
        	        	   ItemCollected.this.finish();
        	           }
        	       });
        	AlertDialog alert = builder.create();
        	alert.show();
//    	unlocker = GlobalData.getInstance().getDbhelper().getUnlocker();
//        if (0==unlocker.size())	{
//        	Toast.makeText(getApplicationContext(), "You have not found any elements. Find them through battles!", Toast.LENGTH_SHORT).show();
        }
    }
}
